package com.aos.erp.schools.rest.student;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.schools.cutils.Constant;
import com.aos.erp.schools.cutils.TimeUtils;
import com.aos.erp.schools.cutils.router.Rest;
import com.aos.erp.schools.enums.student.StudentStatus;
import com.aos.erp.schools.enums.student.SubjectType;
import com.aos.erp.schools.model.coach.Coach;
import com.aos.erp.schools.model.gatherschool.entity.GatherSchool;
import com.aos.erp.schools.model.student.CoachStudent;
import com.aos.erp.schools.model.student.Student;
import com.aos.erp.schools.model.student.StudentWarning;
import com.aos.erp.schools.model.student.ro.coach.AddCoachStudentRo;
import com.aos.erp.schools.model.student.ro.coach.SelectCoachRo;
import com.aos.erp.schools.model.student.ro.student.SelectByStateRo;
import com.aos.erp.schools.model.student.ro.student.UpdateStudentRo;
import com.aos.erp.schools.model.student.ro.warnig.AddWarnigRo;
import com.aos.erp.schools.model.student.vo.coach.CoachVo;
import com.aos.erp.schools.model.student.vo.student.StudentEaxmVo;
import com.aos.erp.schools.service.coach.CoachService;
import com.aos.erp.schools.service.gatherschool.GatherSchoolService;
import com.aos.erp.schools.service.student.*;
import com.aos.erp.schools.util.UserUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: chb
 * @Date: 2018/11/1 19:22
 * @Description:    培训科目教练分配学员
 */
@RestController
@RequestMapping(Rest.CoachStudent.ROOT)
@Api(tags = {"培训科目教练分配学员"})
public class CoachStudentController {

    @Autowired
    private CoachStudentService coachStudentService;

    @Autowired
    private CoachService coachService;

    @Autowired
    private StudentService studentService ;

    @Autowired
    private StudentExamService studentExamService;

    @GetMapping(Rest.CoachStudent.queryStudentCoach)
    @ApiOperation(value = "查询需要分配教练的学员")
    public ResultEntity<PageBean<StudentEaxmVo>> queryStudentCoach(SelectByStateRo ro) {
        try {
            if (StringUtils.isEmpty(ro.getCurrentUserId())) {
                return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
            }
            GatherSchool driversSchool= UserUtil.getUserSchool(ro.getCurrentUserId());
            if(driversSchool==null){
                return ResultEntity.failed("驾校数据不存在");
            }
            ro.setDriversId(driversSchool.getId());
            ro.setMarking("xx");//只是判断 有值  sql 里面判断的
            ro.setSubject(ro.getSubject());

            if(ro.getTimeStart()!=null)
            {
                ro.setTimeType(1);//1表示 考试日期  后期完善
                ro.setTimeStartName(TimeUtils.start(ro.getTimeStart()));
                ro.setTimeEddName(TimeUtils.edd(ro.getTimeStart()));
            }

            PageBean<StudentEaxmVo> student = studentExamService.queryBySubject(ro);
            return ResultEntity.success(student);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("查询失败!");
        }
    }


    @GetMapping(Rest.CoachStudent.getCoachAll)
    @ApiOperation(value = "得到所有的教练")
    public ResultEntity<IPage<Coach>> getCoachAll(SelectCoachRo ro) throws Exception
    {
        if (StringUtils.isEmpty(ro.getCurrentUserId())) {
            return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
        }
        GatherSchool driversSchool= UserUtil.getUserSchool(ro.getCurrentUserId());
        if(driversSchool==null){
            return ResultEntity.failed("驾校数据不存在");
        }
        //用户id
        if (StringUtils.isEmpty(ro.getCurrentUserId())) {
            return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
        }

        String  driversSchoolId= UserUtil.getUserSchoolId(ro.getCurrentUserId());
        //驾校id
        if(StringUtils.isBlank(driversSchoolId)){
            return ResultEntity.failed("驾校为空！不能进行查询操作", ResultEntity.CODE_NULL);
        }
        //得到所有的教练Integer pageIndex,Integer pageSize,String schoolId,String name
        IPage<Coach> listCoach=coachService.queryCoachBySchoolId(ro.getPageIndex(),ro.getPageSize(),driversSchoolId,ro.getName());

        return ResultEntity.success(listCoach);
     }


    @PostMapping(Rest.CoachStudent.add)
    @ApiOperation(value = "培训科目教练分配学员信息添加")
    public ResultEntity addStudentPicture(@RequestBody List<AddCoachStudentRo> ro,@RequestParam(name = "currentUserId", required = false) String currentUserId)throws Exception{

        if (StringUtils.isEmpty(currentUserId)) {
            return ResultEntity.failed("请重新登录", ResultEntity.CODE_NULL);
        }
        GatherSchool driversSchool= UserUtil.getUserSchool(currentUserId);
        if(driversSchool==null){
            return ResultEntity.failed("驾校数据不存在");
        }

        try {
             for(AddCoachStudentRo coach : ro) {
                 CoachStudent sw=new CoachStudent();
                 BeanUtils.copyProperties(coach,sw);
                 sw.setDriversId(driversSchool.getId());
                 sw.setOperatorId(Long.parseLong(currentUserId));
                 coachStudentService.saveBean(sw);

                int  studentId=0;

                 if(coach.getSubject().getDesc()==Constant.k1){  //科目一分配教练
                     studentId=StudentStatus.joink2.getValues();
                 }
                 if(coach.getSubject().getDesc()==Constant.k2){   //科目二分配教练
                     studentId=StudentStatus.joink3.getValues();//变为科目3 在培
                 }
                if(studentId!=0)
                {
                    studentService.updateStudentByStatus(coach.getStudentId(),studentId);
                }




            }
            return ResultEntity.success("培训科目教练分配学员信息添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("培训科目教练分配学员信息添加失败!");
        }

    }


    @PostMapping(Rest.CoachStudent.update)
    @ApiOperation(value = "培训科目教练分配学员信息修改")
    public ResultEntity updateStudentPicture(CoachStudent ro){

        try {
            coachStudentService.updateBean(ro);
            return ResultEntity.success("培训科目教练分配学员修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.failed("培训科目教练分配学员修改失败!");
        }

    }






}
